using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace M1
{
    public class 名片 : MonoBehaviour
    {
        [SerializeField] private Text textName;
        [SerializeField] private GameObject obj他人;
        [SerializeField] private GameObject obj自己;
        [SerializeField] private bool enable;
        [SerializeField] private GameObject 任务标识;
        [SerializeField] private Vector3 偏移 = new Vector3(0,120,0);
        public bool Enable
        {
            get { return enable; }
            set
            {
                enable = value;
                gameObject.SetActive(enable);
            }
        }

        public void 隐藏所有标识()
        {
            任务标识.gameObject.SetActive(false);
        }

        public void 开始任务标识()
        {
            任务标识.gameObject.SetActive(true);
        }

        // private CanvasScaler _canvas;
        private Vector2 _canvasSize;

        public void setName(string s,bool isSelf=false)
        {
            textName.text = s;
            obj他人.SetActive(!isSelf);
            obj自己.SetActive(isSelf);
        }

        private Camera camera;
        public void 跟随(Vector3 transformPosition)
        {
            if (Enable)
            {
                // if (_canvas == null)
                // {
                //     _canvas = transform.GetComponentInParent<CanvasScaler>();
                //     _canvasSize = _canvas.referenceResolution;
                // }

                if (camera == null)
                {
                    camera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
                }
                // var car = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
                Vector2 posscreen = camera.WorldToScreenPoint(transformPosition);

                // Vector3 pos = (Vector2) transformPosition + _canvasSize / 2f;
                transform.position = (Vector3)posscreen+偏移;
                // transform.position = (Vector3)posscreen;
            }
        }
    }
}

